package com.nexus.ai.rag;

import com.nexus.core.properties.ExternalPropertySourceFactory;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.MessageWindowChatMemory;
import org.springframework.ai.chat.memory.repository.jdbc.JdbcChatMemoryRepository;
import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

/**
 * 聊天检索增强配置
 *
 * @Date 2025/9/29 03:49
 * @Author luzhengning
 **/
@Configuration
@PropertySource(name = "rag.properties", value = "classpath*:rag-pro.properties", encoding = "UTF8", factory = ExternalPropertySourceFactory.class, ignoreResourceNotFound = true)
public class NexusAiRagConfig {

    /**
     * Ollama聊天客户端
     *
     * @Date 2025/9/29 03:52
     * @Author luzhengning
     **/
    @Bean
    public ChatClient ollamaChatClient(OllamaChatModel ollamaChatModel) {
        return ChatClient.builder(ollamaChatModel).build();
    }

    /**
     * 聊天记录存储
     *
     * @Date 2025/9/27 21:48
     * @Author luzhengning
     **/
    @Bean
    public ChatMemory chatMemory(JdbcChatMemoryRepository jdbcChatMemoryRepository) {
        ChatMemory chatMemory = MessageWindowChatMemory.builder().chatMemoryRepository(jdbcChatMemoryRepository).maxMessages(10).build();
        return chatMemory;
    }
}
